package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjNAsphaltMatchBo;
import com.ruoyi.zhgd.domain.vo.ZjNAsphaltMatchVo;
import com.ruoyi.zhgd.domain.ZjNAsphaltMatch;
import com.ruoyi.zhgd.mapper.ZjNAsphaltMatchMapper;
import com.ruoyi.zhgd.service.IZjNAsphaltMatchService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 自建-沥青配比信息Service业务层处理
 *
 * @author tll
 * @date 2024-09-26
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjNAsphaltMatchServiceImpl implements IZjNAsphaltMatchService {

    private final ZjNAsphaltMatchMapper baseMapper;

    /**
     * 查询自建-沥青配比信息
     */
    @Override
    public ZjNAsphaltMatchVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询自建-沥青配比信息列表
     */
    @Override
    public TableDataInfo<ZjNAsphaltMatchVo> queryPageList(ZjNAsphaltMatchBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<ZjNAsphaltMatch> lqw = buildQueryWrapper(bo);
//        Page<ZjNAsphaltMatchVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<ZjNAsphaltMatchVo> result = baseMapper.pageVo(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询自建-沥青配比信息列表
     */
    @Override
    public List<ZjNAsphaltMatchVo> queryList(ZjNAsphaltMatchBo bo) {
        LambdaQueryWrapper<ZjNAsphaltMatch> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjNAsphaltMatch> buildQueryWrapper(ZjNAsphaltMatchBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjNAsphaltMatch> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMatchName()), ZjNAsphaltMatch::getMatchName, bo.getMatchName());
        lqw.eq(bo.getCreator() != null, ZjNAsphaltMatch::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, ZjNAsphaltMatch::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, ZjNAsphaltMatch::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, ZjNAsphaltMatch::getUpdateDate, bo.getUpdateDate());
        lqw.eq(bo.getAsphaltTemperatureMin() != null, ZjNAsphaltMatch::getAsphaltTemperatureMin, bo.getAsphaltTemperatureMin());
        lqw.eq(bo.getAsphaltTemperatureMax() != null, ZjNAsphaltMatch::getAsphaltTemperatureMax, bo.getAsphaltTemperatureMax());
        lqw.eq(bo.getAsphaltAggregateRatioMin() != null, ZjNAsphaltMatch::getAsphaltAggregateRatioMin, bo.getAsphaltAggregateRatioMin());
        lqw.eq(bo.getAsphaltAggregateRatioMax() != null, ZjNAsphaltMatch::getAsphaltAggregateRatioMax, bo.getAsphaltAggregateRatioMax());
        lqw.eq(bo.getGravel1Min() != null, ZjNAsphaltMatch::getGravel1Min, bo.getGravel1Min());
        lqw.eq(bo.getGravel1Max() != null, ZjNAsphaltMatch::getGravel1Max, bo.getGravel1Max());
        lqw.eq(bo.getGravel2Min() != null, ZjNAsphaltMatch::getGravel2Min, bo.getGravel2Min());
        lqw.eq(bo.getGravel2Max() != null, ZjNAsphaltMatch::getGravel2Max, bo.getGravel2Max());
        lqw.eq(bo.getGravel3Min() != null, ZjNAsphaltMatch::getGravel3Min, bo.getGravel3Min());
        lqw.eq(bo.getGravel3Max() != null, ZjNAsphaltMatch::getGravel3Max, bo.getGravel3Max());
        lqw.eq(bo.getGravel4Min() != null, ZjNAsphaltMatch::getGravel4Min, bo.getGravel4Min());
        lqw.eq(bo.getGravel4Max() != null, ZjNAsphaltMatch::getGravel4Max, bo.getGravel4Max());
        lqw.eq(bo.getGravel5Min() != null, ZjNAsphaltMatch::getGravel5Min, bo.getGravel5Min());
        lqw.eq(bo.getGravel5Max() != null, ZjNAsphaltMatch::getGravel5Max, bo.getGravel5Max());
        lqw.eq(bo.getGravel6Min() != null, ZjNAsphaltMatch::getGravel6Min, bo.getGravel6Min());
        lqw.eq(bo.getGravel6Max() != null, ZjNAsphaltMatch::getGravel6Max, bo.getGravel6Max());
        lqw.eq(bo.getPowder1Min() != null, ZjNAsphaltMatch::getPowder1Min, bo.getPowder1Min());
        lqw.eq(bo.getPowder1Max() != null, ZjNAsphaltMatch::getPowder1Max, bo.getPowder1Max());
        lqw.eq(bo.getPowder2Min() != null, ZjNAsphaltMatch::getPowder2Min, bo.getPowder2Min());
        lqw.eq(bo.getPowder2Max() != null, ZjNAsphaltMatch::getPowder2Max, bo.getPowder2Max());
        lqw.eq(bo.getPowder3Min() != null, ZjNAsphaltMatch::getPowder3Min, bo.getPowder3Min());
        lqw.eq(bo.getPowder3Max() != null, ZjNAsphaltMatch::getPowder3Max, bo.getPowder3Max());
        lqw.eq(bo.getRecycledMaterialMin() != null, ZjNAsphaltMatch::getRecycledMaterialMin, bo.getRecycledMaterialMin());
        lqw.eq(bo.getRecycledMaterialMax() != null, ZjNAsphaltMatch::getRecycledMaterialMax, bo.getRecycledMaterialMax());
        lqw.eq(bo.getAsphaltMin() != null, ZjNAsphaltMatch::getAsphaltMin, bo.getAsphaltMin());
        lqw.eq(bo.getAsphaltMax() != null, ZjNAsphaltMatch::getAsphaltMax, bo.getAsphaltMax());
        lqw.eq(StringUtils.isNotBlank(bo.getStationCode()), ZjNAsphaltMatch::getStationCode, bo.getStationCode());
        return lqw;
    }

    /**
     * 新增自建-沥青配比信息
     */
    @Override
    public Boolean insertByBo(ZjNAsphaltMatchBo bo) {
        ZjNAsphaltMatch add = BeanUtil.toBean(bo, ZjNAsphaltMatch.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改自建-沥青配比信息
     */
    @Override
    public Boolean updateByBo(ZjNAsphaltMatchBo bo) {
        ZjNAsphaltMatch update = BeanUtil.toBean(bo, ZjNAsphaltMatch.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjNAsphaltMatch entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除自建-沥青配比信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
